﻿using System;

namespace DBMock.Builders.Attributes
{
    /// <summary>
    /// Builder特性基类
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
    public class BaseBuilderAttribute : BuilderAttribute
    {
        protected Type type; //builder类型
        protected object[] para; //builder构造函数参数

        /// <summary>
        /// 初始化BaseBuilderAttribute
        /// </summary>
        /// <param name="builderType">builder类型</param>
        /// <param name="builderParams">builder构造函数参数</param>
        public BaseBuilderAttribute(Type builderType, params object[] builderParams)
        {
            type = builderType;
            para = builderParams;
        }

        public override Builder GetBuilder(int seed)
        {
            if (type == null) return null;

            var newPara = new object[(para?.Length ?? 0) + 1];
            newPara[0] = seed;

            for (int i = 0; i < newPara.Length - 1; i++)
            { newPara[i + 1] = para[i]; }

            var builder = Activator.CreateInstance(type, newPara) as Builder;
            if (builder == null) return null;

            builder.NullPercent = NullPercent;

            return builder;
        }
    }
}
